use axum::Json;
use serde_json::Value;

use crate::{encrypt::sha1, query};

pub fn token(tok:&str, param:Value) -> Result<String, Json<Value>>{
    let timestamp = query::str(&param, "timestamp")?;
    let nonce = query::str(&param, "nonce")?;
    let signature = query::str(&param, "signature")?;
    let echostr = query::str(&param, "echostr")?;

    let sign = sha1::encode(&format!("{tok}{timestamp}{nonce}"));

    if sign == signature {
        Ok(echostr)
    }else{
        Ok(String::default())
    }
}